package com.tmall.wireless.virtualviewdemo.utils.imageloader.core.download;

/*******************************************************************************
 * Copyright 2011-2014 Sergey Tarasevich
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/


import com.tmall.wireless.virtualviewdemo.utils.imageloader.core.assist.ContentLengthInputStream;
import com.tmall.wireless.virtualviewdemo.utils.imageloader.utils.IoUtils;
import com.tmall.wireless.virtualviewdemo.utils.imageloader.utils.L;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.utils.net.Uri;
import okhttp3.*;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * Provides retrieving of {@link InputStream} of image by URI from network or file system or app resources.<br />
 * {@link URLConnection} is used to retrieve image stream from network.
 *
 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 * @since 1.8.0
 */
public class BaseImageDownloader implements ImageDownloader {
    /**
     * {@value}
     */
    public static final int DEFAULT_HTTP_CONNECT_TIMEOUT = 5 * 1000; // milliseconds
    /**
     * {@value}
     */
    public static final int DEFAULT_HTTP_READ_TIMEOUT = 20 * 1000; // milliseconds

    /**
     * {@value}
     */
    protected static final int BUFFER_SIZE = 32 * 1024; // 32 Kb
    /**
     * {@value}
     */
    protected static final String ALLOWED_URI_CHARS = "@#&=*+-_.,:!?()/~'%";

    protected static final int MAX_REDIRECT_COUNT = 5;


    private static final String ERROR_UNSUPPORTED_SCHEME = "UIL doesn't support scheme(protocol) by default [%s]. " + "You should implement this support yourself (BaseImageDownloader.getStreamFromOtherSource(...))";

    protected final Context context;
    protected final int connectTimeout = DEFAULT_HTTP_CONNECT_TIMEOUT;
    protected final int readTimeout = DEFAULT_HTTP_READ_TIMEOUT;
    private OkHttpClient okHttpClient;

    public BaseImageDownloader(Context context, OkHttpClient okHttpClient) {
        this.context = context.getApplicationContext();
        this.okHttpClient = okHttpClient;
    }


    @Override
    public InputStream getStream(String imageUri, Object extra) throws IOException {
        L.i("imageUri --- " + imageUri);
        switch (Scheme.ofUri(imageUri)) {
            case HTTP:
            case HTTPS:
                return getStreamFromNetwork(imageUri);
            case FILE:
                return getStreamFromFile(imageUri);
            case CONTENT:
                return getStreamFromContent(imageUri);
            case ASSETS:
                return getStreamFromAssets(imageUri);
            case DRAWABLE:
                return getStreamFromDrawable(imageUri);
            case UNKNOWN:
            default:
                return getStreamFromOtherSource(imageUri);
        }
    }

    /**
     * Retrieves {@link InputStream} of image by URI (image is located in the network).
     *
     * @param imageUri Image URI
     *                 DisplayImageOptions.extraForDownloader(Object)}; can be null
     * @return {@link InputStream} of image
     * @throws IOException if some I/O error occurs during network request or if no InputStream could be created for
     *                     URL.
     */
    protected InputStream getStreamFromNetwork(String imageUri) throws IOException {

        Response execute = newCall(imageUri).execute();
        int code = execute.code();
        int redirectCount = 0;
        while (code / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
            execute = newCall(execute.headers().get("Location")).execute();
            redirectCount++;
        }
//        HttpURLConnection conn = createConnection(imageUri, extra);
//        int redirectCount = 0;
//        while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
//            conn = createConnection(conn.getHeaderField("Location"), extra);
//            redirectCount++;
//        }
        ResponseBody body = execute.body();
        InputStream imageStream = body.byteStream();

//        try {
//            imageStream = conn.getInputStream();
//        } catch (IOException e) {
//            // Read all data to allow reuse connection (http://bit.ly/1ad35PY)
//            IoUtils.readAndCloseStream(conn.getErrorStream());
//            throw e;
//        }
        //   if (!shouldBeProcessed(conn)) {
        if (!(code == 200)) {
            IoUtils.closeSilently(imageStream);
            throw new IOException("Image request failed with response code " + code);
        }
        return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), (int) body.contentLength());
    }

    /**
     * 每次下载需要新建新的Call对象
     *
     * @param url 请求的链接
     * @return Call
     */
    private Call newCall(String url) {
        Request request = new Request.Builder()
                .url(url)
                .build();

        return okHttpClient.newCall(request);
    }


    /**
     * shouldBeProcessed
     * @param conn Opened request connection (response code is available)
     * @return <b>true</b> - if data from connection is correct and should be read and processed;
     * <b>false</b> - if response contains irrelevant data and shouldn't be processed
     * @throws IOException
     */
    protected boolean shouldBeProcessed(HttpURLConnection conn) throws IOException {
        return conn.getResponseCode() == 200;
    }

    /**
     * Create {@linkplain HttpURLConnection HTTP connection} for incoming URL
     *
     * @param url URL to connect to
     *            DisplayImageOptions.extraForDownloader(Object)}; can be null
     * @return {@linkplain HttpURLConnection Connection} for incoming URL. Connection isn't established so it still configurable.
     * @throws IOException if some I/O error occurs during network request or if no InputStream could be created for
     *                     URL.
     */
    protected HttpURLConnection createConnection(String url) throws IOException {
        String encodedUrl = Uri.encode(url, ALLOWED_URI_CHARS);
        HttpURLConnection conn = (HttpURLConnection) new URL(encodedUrl).openConnection();
        conn.setConnectTimeout(connectTimeout);
        conn.setReadTimeout(readTimeout);
        return conn;
    }

    /**
     * Retrieves {@link InputStream} of image by URI (image is located on the local file system or SD card).
     *
     * @param imageUri Image URI
     *                 DisplayImageOptions.extraForDownloader(Object)}; can be null
     * @return {@link InputStream} of image
     * @throws IOException if some I/O error occurs reading from file system
     */
    protected InputStream getStreamFromFile(String imageUri) throws IOException {
        String filePath = Scheme.FILE.crop(imageUri);
        if (isVideoFileUri(imageUri)) {
            return getVideoThumbnailStream(filePath);
        } else {
            BufferedInputStream imageStream = new BufferedInputStream(new FileInputStream(filePath), BUFFER_SIZE);
            return new ContentLengthInputStream(imageStream, (int) new File(filePath).length());
        }
    }

    private InputStream getVideoThumbnailStream(String filePath) {
//        Bitmap bitmap = ThumbnailUtils
//                .createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
//        if (bitmap != null) {
//            ByteArrayOutputStream bos = new ByteArrayOutputStream();
//            bitmap.compress(CompressFormat.PNG, 0, bos);
//            return new ByteArrayInputStream(bos.toByteArray());
//        }
        return null;
    }


    protected InputStream getStreamFromContent(String imageUri) throws FileNotFoundException {
//        ContentResolver res = context.getContentResolver();
//
//        Uri uri = Uri.parse(imageUri);
//        if (isVideoContentUri(uri)) { // video thumbnail
//            Long origId = Long.valueOf(uri.getLastPathSegment());
//            Bitmap bitmap = MediaStore.Video.Thumbnails
//                    .getThumbnail(res, origId, MediaStore.Images.Thumbnails.MINI_KIND, null);
//            if (bitmap != null) {
//                ByteArrayOutputStream bos = new ByteArrayOutputStream();
//                bitmap.compress(CompressFormat.PNG, 0, bos);
//                return new ByteArrayInputStream(bos.toByteArray());
//            }
//        } else if (imageUri.startsWith(CONTENT_CONTACTS_URI_PREFIX)) { // contacts photo
//            return getContactPhotoStream(uri);
//        }
//
//        return res.openInputStream(uri);
        return null;
    }

    protected InputStream getContactPhotoStream(Uri uri) {
//        ContentResolver res = context.getContentResolver();
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
//            return ContactsContract.Contacts.openContactPhotoInputStream(res, uri, true);
//        } else {
//            return ContactsContract.Contacts.openContactPhotoInputStream(res, uri);
//        }
        return null;
    }


    protected InputStream getStreamFromAssets(String imageUri) throws IOException {
        String filePath = Scheme.ASSETS.crop(imageUri);
        //  return context.getAssets().open(filePath);
        return null;
    }

    /**
     * Retrieves {@link InputStream} of image by URI (image is located in drawable resources of application).
     *
     * @param imageUri Image URI
     *                 DisplayImageOptions.extraForDownloader(Object)}; can be null
     * @return {@link InputStream} of image
     */
    protected InputStream getStreamFromDrawable(String imageUri) {
        String drawableIdString = Scheme.DRAWABLE.crop(imageUri);
        int drawableId = Integer.parseInt(drawableIdString);
        //  return context.getResources().openRawResource(drawableId);
        try {
            return context.getResourceManager().getResource(drawableId);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Retrieves {@link InputStream} of image by URI from other source with unsupported scheme. Should be overriden by
     * successors to implement image downloading from special sources.<br />
     * This method is called only if image URI has unsupported scheme. Throws {@link UnsupportedOperationException} by
     * default.
     *
     * @param imageUri Image URI
     * @return {@link InputStream} of image
     * @throws IOException                   if some I/O error occurs
     * @throws UnsupportedOperationException if image URI has unsupported scheme(protocol)
     */
    protected InputStream getStreamFromOtherSource(String imageUri) throws IOException {
        throw new UnsupportedOperationException(String.format(ERROR_UNSUPPORTED_SCHEME, imageUri));
    }

    private boolean isVideoContentUri(Uri uri) {
        //String mimeType = context.getContentResolver().getType(uri);
        // return mimeType != null && mimeType.startsWith("video/");
        return false;
    }

    private boolean isVideoFileUri(String uri) {
//        String extension = MimeTypeMap.getFileExtensionFromUrl(uri);
//        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
//        return mimeType != null && mimeType.startsWith("video/");
        return false;
    }
}

